#include <network_api.h>
#include <logger.h>
#include <object2asn.h>
#include <configer.h>
#include <security.h>
#include "tc.h"
#include "app_fcw.h"
#include <web_connect.h>
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <unistd.h>
#ifdef SIL_TEST
#include <SIL_test.h>
#endif
#define MAP_AID 17
#define SPAT_AID 17
#define RSI_AID 17
#define SEND_BUF_LEN 1024
#define SEND_BUF_MAP_LEN 202400
#ifndef MILLSEC
#define MILLSEC 1000
#endif

#define CERT_SERVER_IP "222.168.43.74"
#define AC_PORT 10006
#define PC_PORT 10007
#define IC_PORT 10006
#define EC_PORT 10005

void network_cb_function_for_test(v2x_message_frame_t* data){
    printf("new message!\n");
}

int main(){
    config_initial();
    // web_connect_init();
    #ifdef SIL_TEST
    config.enable_sucurity=1;
    set_SecurityUrl(CERT_SERVER_IP,EC_PORT,PC_PORT,AC_PORT,IC_PORT);
    security_init();
    #endif
    #ifndef SIL_TEST
    config.enable_sucurity=0;
    set_SecurityUrl(CERT_SERVER_IP,EC_PORT,PC_PORT,AC_PORT,IC_PORT);
    // security_init();
    #endif
    #ifdef SIL_TEST
    SIL_config_t sc;
    sc.tps.bindPort=1208;
    sc.tps.dataType=e_DataType_XML;
    sc.wms.bindPort=1209;
    sc.wms.dataType=e_DataType_XML;
    v2x_sdk_code_t ret = v2x_sdk_init_forSIL(sc);
    #else
    v2x_sdk_code_t ret = v2x_sdk_init();
    #endif

    //v2x_sdk_code_t ret=v2x_sdk_init_max_rate();
    if(ret == v2x_OK){
        log_print(LOG_INFO,"v2x_sdk_init finished!");
    }  
    else{
        log_print(LOG_ERROR,"v2x_sdk_init error!");
    }
    dsm_req_info_t dsm_req;
    dsm_req.action=DSM_SERVICE_TYPE_ADD;
    aid_info_t aidinfo;
    aidinfo.aid=AID;
    aidinfo.dsm_onrcv=network_cb_function_for_test;
    aidinfo.messageSetEnable=BSM_ENABLE;
    dsm_req.aidinfo=aidinfo;
    v2x_dsm_service_req(dsm_req);
    access_layer_tx_data_t* tx_data=(access_layer_tx_data_t*)malloc(sizeof(access_layer_tx_data_t));
    tx_data->choice=BSM_ENABLE;
    MessageFrame_t* mf=basicInitial_forTest();
    // len_buf_t* lb=object2asn(mf);
    // tx_data->data=lb->buf;
    // tx_data->data_len=lb->len;
    pthread_t t1,t2,t3;
    // pthread_create(&t1,NULL,send_RSI,NULL);
    pthread_create(&t1,NULL,send_SPAT,NULL);
    // pthread_create(&t1,NULL,send_MAP,NULL);
    while(1){
        usleep(100*MILLSEC);
        // v2x_sdk_network_send_data(tx_data,17);
        // log_print(LOG_DEBUG,"map_freq:%d,rsi_freq:%d,spat_freq:%d",config.send_frequency_map,config.send_frequency_rsi,config.send_frequency_spat);
    }
}